package com.minyifang.wcspf.work.ui.user.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.ui.user.model.CouponBean;
import com.minyifang.wcspf.work.ui.user.view.CouponView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/6/27.
 */
public class CouponPresenter extends BasePresenter<CouponView> {
    public void getCouponList(int pageIndex, String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", pageIndex);
        map.put("type", type);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getCouponList(requestBody), respond -> {
            if (respond.result == 1) {
                LogUtil.e("优惠券数据 :" + SafeUtils.decrypt(respond.data));
                List<CouponBean> list = new Gson().fromJson(SafeUtils.decrypt(respond.data), new TypeToken<List<CouponBean>>() {
                }.getType());
                getView().showCouponList(list);
            } else
                getView().showRequestFailed(respond.message);
        });
    }

    public void deleteCoupon(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("couponid", id);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().deleteCoupon(requestBody), respond -> {
            if (respond.result == 1) {
                getView().showDelCouponRes(respond.message);
            } else
                getView().showRequestFailed(respond.message);
        });
    }
}
